package com.ribbon;

import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * Created by qhe on 2018/7/26.
 *
 * 创建请求命令
 * 继承的方式实现封装具体的依赖服务调用逻辑
 *
 */
public class UserCommand extends HystrixCommand<User> {

    private RestTemplate restTemplate;

    private Long id;

    public UserCommand(Setter setter,RestTemplate restTemplate,Long id){
        super(setter);
        this.restTemplate = restTemplate;
        this.id = id;
    }

    @Override
    protected User run() throws Exception {
        return restTemplate.getForObject("Http://user-service/hello/user/{1}",User.class,id);
    }

    /**
     * 服务降级
     * @return
     */
    @Override
    protected User getFallback(){
        return new User();
    }

    /**
     * 开始请求缓存
     * @return
     */
    @Override
    protected String getCacheKey(){
        return String.valueOf(id);
    }


}
